package com.msh.artascope.devcloud.service.service.impl;


import com.msh.artascope.devcloud.service.dao.LogConfigDao;
import com.msh.artascope.devcloud.service.model.query.LogConfigQueryVo;
import com.msh.artascope.devcloud.service.model.vo.LogConfigVo;
import javax.annotation.Resource;

import com.msh.frame.client.base.TenantBaseServiceImpl;
import com.msh.frame.interfaces.IdGenerateable;
import com.msh.artascope.devcloud.client.po.LogConfigPo;
import com.msh.artascope.devcloud.client.qo.LogConfigQo;
import com.msh.artascope.devcloud.service.service.LogConfigService;
import com.msh.frame.client.common.Result;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import org.springframework.util.CollectionUtils;

/**
 * @author shihu
 * @mail m-sh@qq.com
 */
@Service
@Slf4j
public class LogConfigServiceImpl extends TenantBaseServiceImpl<LogConfigPo,LogConfigQo> implements LogConfigService {

  @Resource
  private LogConfigDao logConfigDao;

  @Resource
  private IdGenerateable idGenerateable;

  @Override
  public long insert(LogConfigPo param) {
    param.setId(idGenerateable.getUniqueId());
    return super.insert(param);
  }

  @Override
  public int insertCollection(Collection<LogConfigPo> param) {
    param.stream().forEach(p->p.setId(idGenerateable.getUniqueId()));
    return super.insertCollection(param);
  }


  @Override
  public LogConfigPo getByEnvId(Long envId, Long tenantId) {
    return listOne(new LogConfigQo().setEnvId(envId).setTenantId(tenantId));
  }



// ----------前端vo处理-------------------------------------------------------

  private LogConfigVo toVo(LogConfigPo po){
    LogConfigVo vo = po.convert(LogConfigVo.class);
    return vo;
  }

  private List<LogConfigVo> toVoList(List<LogConfigPo> list){
    if(CollectionUtils.isEmpty(list)){
      return Collections.emptyList();
    }

    return list.stream().map(po->{
      return toVo(po);
    }).collect(Collectors.toList());
  }

  @Override
  public LogConfigVo getVo(Long id, Long tenantId) {
    LogConfigPo po = this.get(id, tenantId);
    if(null == po){
      return null;
    }
    LogConfigVo vo = toVo(po);

    return vo;
  }

  @Override
  public List<LogConfigVo> listVo(LogConfigQueryVo q){
    LogConfigQo qo = q.convert(LogConfigQo.class);
    List<LogConfigPo> poList = this.list(qo);
    List<LogConfigVo> voList = toVoList(poList);

    return voList;
  }

  @Override
  public Result<List<LogConfigVo>> pageVo(LogConfigQueryVo param) {
    LogConfigQo qo = param.convert(LogConfigQo.class);
    int count = this.count(qo);
    if (0 == count) {
      return Result.successReturn(Collections.<LogConfigVo>emptyList()).setCount(count);
    }
    List<LogConfigPo> poList = this.list(qo);
    List<LogConfigVo> voList = toVoList(poList);

    return Result.successReturn(voList).setCount(count);
  }


}
